package com.sunlx.product.generatesql;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.sunlx.product.dictionary.AlterTableTypeEnum;
import com.sunlx.product.dictionary.DatabaseTwoEnum;
import com.sunlx.product.domain.entity.ColumnDll;
import com.sunlx.product.domain.entity.TableDll;
import com.sunlx.product.domain.vo.TableDataVo;
import com.sunlx.product.mapper.MyDb1.MyDb1Mapper;
import com.sunlx.product.mapper.MyDb2.MyDb2Mapper;
import com.sunlx.product.utils.GeneColumnDllSqlUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import javax.annotation.Resource;
import java.util.List;

/**
 * TODO
 *
 * @author Sunluxin
 * @date 2023/1/12 15:30
 */
public class OracleSqlNormal implements SqlStrategy{

    @Value("${spring.datasource.MyDb2.username}")
    private String userName2;
    @Value("${spring.datasource.MyDb1.username}")
    private String userName1;

    @Resource
    private MyDb1Mapper myDb1Mapper;
    @Resource
    private MyDb2Mapper myDb2Mapper;

    /**
     * 查询数据库的表结构及字段信息
     * @param databaseTwoEnum
     * @return {@link List< TableDataVo>}
     * @author Sunluxin
     * @date 2023/1/13 15:07 
    */
    @Override
    public List<TableDataVo> selectTableData(DatabaseTwoEnum databaseTwoEnum) {
        if (DatabaseTwoEnum.MAIN.equals(databaseTwoEnum)){
            List<TableDataVo> mainTableDataVos = myDb1Mapper.selectTableData_Oracle();
            return mainTableDataVos;
        }else {
            List<TableDataVo> mainTableDataVos = myDb2Mapper.selectTableData_Oracle();
            return mainTableDataVos;
        }
    }

    /**
     * 查找表的dll
     * @param tableName
     * @return {@link TableDll}
     * @author Sunluxin
     * @date 2023/1/13 15:07 
    */
    @Override
    public TableDll selectTableDll(String tableName) {
        TableDll dll=new TableDll();
        List<TableDll> tableDlls = myDb2Mapper.selectTableDll_oracle(tableName, userName2);
        if(CollectionUtils.isNotEmpty(tableDlls)){
            dll=tableDlls.get(0);
        }
        return dll;
    }

    @Override
    public String getUserName(String type) {
        if("userName1".equals(type)){
            return userName1;
        }else if("userName2".equals(type)){
            return userName2;
        }
        return "";
    }

    /**
     * 生成字段的新增、修改、删除语句
     * @param tableDataVo
     * @param alterTableTypeEnum
     * @return {@link ColumnDll}
     * @author Sunluxin
     * @date 2023/1/13 15:07
    */
    @Override
    public ColumnDll geneSql(TableDataVo tableDataVo, AlterTableTypeEnum alterTableTypeEnum,Boolean onlyComments) {
        String sql="";
        if(AlterTableTypeEnum.DELETE.equals(alterTableTypeEnum)){
            sql="alter table "+tableDataVo.getTabelName()+" drop ("+tableDataVo.getColumnName()+")";
        }else{
            sql= GeneColumnDllSqlUtils.geneSqlOralce(tableDataVo,alterTableTypeEnum);
        }
        if(onlyComments){
            //comment on column BA_EN_METER_PRICE_REL.createuname is '?????'
            sql=" comment on column "+tableDataVo.getTabelName()+"."+tableDataVo.getColumnName()+" is '"+tableDataVo.getComments()+"' ";
        }
        return new ColumnDll(tableDataVo.getTabelName(),tableDataVo.getColumnName(),sql,alterTableTypeEnum.getLabel());
    }

    
}
